package com.generator;

import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.mybatis.generator.api.GeneratedJavaFile;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateExceptionHandler;

public class UiPlugin extends PluginAdapter{

	String jsDir=ServicePlugin.rootDir+"resources\\static\\js";
	String htmlDir=ServicePlugin.rootDir+"resources\\templates\\iframe";
	
	Configuration cfg;
	public UiPlugin() {
		super();
		cfg = new Configuration(Configuration.VERSION_2_3_22);
		cfg.setClassForTemplateLoading(this.getClass(),"/FreeMarker");
		cfg.setDefaultEncoding("UTF-8");
		cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
	}
	
	@Override
	public boolean validate(List<String> warnings) {
		return true;
	}
	@Override
    public List<GeneratedJavaFile> contextGenerateAdditionalJavaFiles(IntrospectedTable introspectedTable) {
		List<GeneratedJavaFile> mapperJavaFiles = new ArrayList<GeneratedJavaFile>();
    	String domainObjectName=introspectedTable.getFullyQualifiedTable().getDomainObjectName();
    	String lowerDomainObjectName = StrUtil.lowerFirst(domainObjectName);
    	List<IntrospectedColumn> pk = introspectedTable.getPrimaryKeyColumns();
    	List<String> columns = introspectedTable.getAllColumns().stream()
    			.map(IntrospectedColumn::getJavaProperty)
    			.filter(c -> { return !pk.stream().filter(p -> {return p.getJavaProperty().equals(c);}).findFirst().isPresent(); })
    			.collect(Collectors.toList());
    	try {
			Map<String, Object> prop = new HashMap<>();
			prop.put("domainObjectName", domainObjectName);
			prop.put("lowerDomainObjectName", lowerDomainObjectName);
			if(pk.size() == 1) {
				prop.put("pk", StrUtil.upperFirst(pk.get(0).getJavaProperty()));
				prop.put("lowerPk", pk.get(0).getJavaProperty());
	    	}else {
				prop.put("pk", "pk");
				prop.put("lowerPk", "lowerPk");
	    	}

			prop.put("columns", columns);
			
			Template temp = cfg.getTemplate("UI/JS/list.js");
			BufferedWriter outputStream = FileUtil.getWriter(StrUtil.format("{}/{}/list.js", jsDir,lowerDomainObjectName),Charset.forName("UTF-8"),false);
			temp.process(prop, outputStream);
			outputStream.close();
			

			temp = cfg.getTemplate("UI/JS/object.js");
			outputStream = FileUtil.getWriter(StrUtil.format("{}/{}/{}.js", jsDir,lowerDomainObjectName,lowerDomainObjectName),Charset.forName("UTF-8"),false);
			temp.process(prop, outputStream);
			outputStream.close();
			

			temp = cfg.getTemplate("UI/JS/edit.js");
			outputStream = FileUtil.getWriter(StrUtil.format("{}/{}/edit.js", jsDir,lowerDomainObjectName),Charset.forName("UTF-8"),false);
			temp.process(prop, outputStream);
			outputStream.close();
			

			temp = cfg.getTemplate("UI/HTML/edit.html");
			outputStream = FileUtil.getWriter(StrUtil.format("{}/{}/edit.html", htmlDir,lowerDomainObjectName),Charset.forName("UTF-8"),false);
			temp.process(prop, outputStream);
			outputStream.close();
			

			temp = cfg.getTemplate("UI/HTML/list.html");
			outputStream = FileUtil.getWriter(StrUtil.format("{}/{}/list.html", htmlDir,lowerDomainObjectName),Charset.forName("UTF-8"),false);
			temp.process(prop, outputStream);
			outputStream.close();
			
		}catch(Exception e){
			e.printStackTrace();
		}
		return mapperJavaFiles;
    	
	}
}
